package eclipse.util;
import java.io.*;
import java.net.*;
import eclipse.*;
import eclipse.resources.*;
/**
 *Eclipse Resource Location
 *Points to the Streamed source of an Object.
 */
public class ERL 
{
	private enum ERLTYPE {LOCAL, ARCHIVE, EXTERNAL}
	private String sERL;
	private ERLTYPE tERL;
	/**
	 * Creates an ERL from a path represented by a String.
	 * @param nERL path as String
	 */
	public ERL(String nERL)
	{
		sERL = nERL;
		if(sERL.startsWith("http://"))
			tERL = ERLTYPE.EXTERNAL;
		else if(sERL.startsWith("jar://"))
		{
			tERL = ERLTYPE.ARCHIVE;
			sERL = sERL.replace("jar://", "");
		}
		else
			tERL = ERLTYPE.LOCAL;
	}
	/**
	 * Opens the InputStream for the file being streamed.
	 */
	public InputStream openStream()
	{
		InputStream returnStream = null;
		try
		{
			if(		tERL == ERLTYPE.ARCHIVE)
				returnStream = ResourceManager.getInputStream(sERL);
			else if(tERL == ERLTYPE.EXTERNAL)
				returnStream = new URL(sERL).openStream();
			else if(tERL == ERLTYPE.LOCAL)
				returnStream = new FileInputStream(sERL);
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
		return returnStream;
	}
}
